package com.xs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xs.common.JsonResult;
import com.xs.entity.Announcement;
import com.xs.service.AnnouncementService;
import com.xs.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announceService;

    @Autowired
    private ArticleService articleService;

    /**
     * 查询轮播图信息
     */
    @GetMapping
    public JsonResult<?> getAll(){
        return JsonResult.success(announceService.list());
    }

    /**
     * 新增
     * @param announcement
     * @return
     */
    @PostMapping
    public JsonResult<?> save(@RequestBody Announcement announcement){
        QueryWrapper<Announcement> wrapper=new QueryWrapper<>();
        wrapper.eq("aid",announcement.getAid());
        if (announceService.getOne(wrapper)!=null){
            return JsonResult.success("-1","此文章张已经是轮播图了,请勿重复");
        }
        int count = announceService.count();
        if (count>=5){
            return JsonResult.success("-1","轮播图达到上限");
        }
        System.out.println("数量为："+count);
        announceService.save(announcement);
        return JsonResult.success();
    }

    @DeleteMapping("/{id}")
    public JsonResult<?> delete(@PathVariable Integer id){
        announceService.removeById(id);
        return JsonResult.success();
    }
}
